package com.das.mechanic_base.adapter.create;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.create.ServiceInfoBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.widget.X3MarqueeText;
import com.das.mechanic_base.widget.X3ModifyHeaderView;
import com.kproduce.roundcorners.RoundImageView;

public class ServiceInfoCarUserAdapter extends RecyclerView.Adapter<ServiceInfoCarUserAdapter.ServiceInfoCarUserHolder> implements View.OnClickListener {

    private ServiceInfoBean infoBean;

    private Context mContext;

    public ServiceInfoCarUserAdapter(Context mContext) {
        this.mContext = mContext;
        this.infoBean = new ServiceInfoBean();
    }

    public void changeServiceInfo(ServiceInfoBean infoBean) {
        this.infoBean = infoBean;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ServiceInfoCarUserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ServiceInfoCarUserHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_service_info_car_user_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceInfoCarUserHolder holder, int position) {

        if (position == 0) {
            holder.iv_img.setRadius(0);
            X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(infoBean.carBrandUrl),holder.iv_img, R.mipmap.x3_car_icon_no);

            String carBrandName = X3StringUtils.isEmpty(infoBean.carBrandName) ? "-" : infoBean.carBrandName;
            String carStyleName = X3StringUtils.isEmpty(infoBean.carStyleName) ? "-" : infoBean.carStyleName;

            holder.tv_name.setText(carBrandName + " " + carStyleName);

            holder.tv_car_num.setText(X3StringUtils.isEmpty(infoBean.carNum) ? "" : infoBean.carNum);

            holder.tv_car_num.setTextColor(ContextCompat.getColor(mContext,R.color.bg_707380));

            holder.iv_icon.setImageResource(R.mipmap.service_order_car_icon);

            holder.tv_one.setText(mContext.getString(R.string.service_plan));
            holder.tv_one.setOnClickListener(this);
            holder.tv_content_one.setOnClickListener(this);
            holder.tv_two.setText(mContext.getString(R.string.x3_car_question));

            holder.tv_content_one.setText(infoBean.maintenancePlanAmount + "");

            holder.tv_content_two.setText(infoBean.questionAmount + "");

        }else {
            holder.iv_img.setRadius(40);
            String imgUrl = infoBean.imgUrl;
            if (X3StringUtils.isEmpty(imgUrl)) {
                imgUrl = X3ModifyHeaderView.setName(infoBean.carOwnerName,"",mContext);
                X3GlideNewUtils.loadCircleImage(mContext,imgUrl,holder.iv_img, R.mipmap.x3_car_owner_header);
            }else {
                X3GlideNewUtils.loadCircleImage(mContext,X3StringUtils.getImageUrl(imgUrl),holder.iv_img, R.mipmap.x3_car_owner_header);
            }

            holder.tv_name.setText(X3StringUtils.isEmpty(infoBean.carOwnerName) ? "" : infoBean.carOwnerName);

            holder.tv_car_num.setText(X3StringUtils.isEmpty(infoBean.mobile) ? mContext.getString(R.string.car_not_bind_user) : infoBean.mobile);
            holder.tv_car_num.setTextColor(X3StringUtils.isEmpty(infoBean.mobile) ?
                    ContextCompat.getColor(mContext,R.color.bg_F64444) : ContextCompat.getColor(mContext,R.color.bg_707380));

            holder.iv_icon.setImageResource(R.mipmap.service_order_user_icon);

            holder.tv_one.setText(mContext.getString(R.string.x3_todo_record));

            holder.tv_two.setText(mContext.getString(R.string.x3_user_interactive));

            holder.tv_content_one.setText(infoBean.todoAmount + "");

            holder.tv_content_two.setText(infoBean.communicationAmount + "");
        }

    }

    @Override
    public int getItemCount() {
        return 2;
    }

    IOnClickCarUser iOnClickCarUser;

    public ServiceInfoCarUserAdapter setiOnClickCarUser(IOnClickCarUser iOnClickCarUser) {
        this.iOnClickCarUser = iOnClickCarUser;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_one || v.getId() == R.id.tv_content_one){
            if (iOnClickCarUser != null) {
                iOnClickCarUser.onSubViewClick(0);
            }
        }
    }

    public interface IOnClickCarUser {
        void iOnClickCarUser(int position);
        void onSubViewClick(int type);
    }

    class ServiceInfoCarUserHolder extends RecyclerView.ViewHolder {
        RoundImageView iv_img;
        X3MarqueeText tv_name;
        X3MarqueeText tv_car_num;
        TextView tv_one;
        TextView tv_two;
        TextView tv_content_one;
        TextView tv_content_two;
        ImageView iv_icon;

        public ServiceInfoCarUserHolder(@NonNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_car_num = itemView.findViewById(R.id.tv_car_num);
            tv_one = itemView.findViewById(R.id.tv_one);
            tv_two = itemView.findViewById(R.id.tv_two);
            tv_content_one = itemView.findViewById(R.id.tv_content_one);
            tv_content_two = itemView.findViewById(R.id.tv_content_two);
            iv_icon = itemView.findViewById(R.id.iv_icon);

            itemView.setOnClickListener(v -> {
                int position = getAbsoluteAdapterPosition();
                if (iOnClickCarUser != null) {
                    iOnClickCarUser.iOnClickCarUser(position);
                }
            });
        }
    }
}
